
package de.swta.freezay.database.utilities;

import de.swta.freezay.database.JPAController;
import de.swta.freezay.database.controller.exceptions.IllegalOrphanException;
import de.swta.freezay.database.controller.exceptions.NonexistentEntityException;
import de.swta.freezay.database.controller.exceptions.PreexistingEntityException;
import de.swta.freezay.database.dbEntities.Category;
import de.swta.freezay.database.dbEntities.Item;
import de.swta.freezay.database.dbEntities.ItemPackage;
import de.swta.freezay.database.dbEntities.Unit;
import de.swta.freezay.database.dbEntities.Package;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * --- ONLY FOR DEVELOPING/TESTING PURPOSE ---
 *
 * @author Christian Baeumer
 */
public class DBFiller {

    static Random randomizer = new Random();
    
    private static void createCategories() {

        System.err.println("CREATING CATEGORIES");

        Category newCategory;
        
        for (int i = 0; i < 20; i++) {

            newCategory = new Category();
            newCategory.setName("Category " + i);

            try {
               JPAController.CategoryJpaController.create(newCategory);
            } catch (Exception ex) {
                Logger.getLogger(DBFiller.class.getName()).log(
                        Level.SEVERE, null, ex);
            }
        }
        
    }


    private static void createUnits() {

        System.err.println("CREATING UNITS");

        Unit newUnit;

        for (int i = 0; i < 10; i++) {

            newUnit = new Unit();
            newUnit.setName("Unit " + i);

            try {
                JPAController.UnitJpaController.create(newUnit);
            } catch (Exception ex) {
                Logger.getLogger(DBFiller.class.getName()).log(
                        Level.SEVERE, null, ex);
            }
        }
    }

    private static void createItems() {

        System.err.println("CREATING ITEMS");

        List<Category> categories 
                = JPAController.CategoryJpaController.findCategoryEntities();
        int catCount = categories.size();
        List<Unit> units = JPAController.UnitJpaController.findUnitEntities();
        int unitCount = units.size();
        
        Item newItem;
        

        for (int i = 0; i < 100; i++) {
            newItem = new Item();
            newItem.setName("Item " + i);
            newItem.setAmount(randomizer.nextDouble() * 100);
            newItem.setPrice(randomizer.nextDouble() * 100);
            newItem.setCategory(categories.get(randomizer.nextInt(catCount)));
            newItem.setCount(randomizer.nextInt(20));
            newItem.setUnit(units.get(randomizer.nextInt(unitCount)));

            newItem.setDescription("That's the description of " + newItem.getName() + ".");
            try {
                JPAController.ItemJpaController.create(newItem);
            } catch (Exception ex) {
                Logger.getLogger(DBFiller.class.getName()).log(
                        Level.SEVERE, null, ex);
            }
        }

    }


    private static List<Item> getRandomItems() {

        List<Item> randomItems = new ArrayList<Item>();
        List<Item> items = JPAController.ItemJpaController.findItemEntities();
        int itemCount = items.size();

        int count = randomizer.nextInt(10) + 1;

        for (int i = 0; i < count; i++) {
            randomItems.add(items.get(randomizer.nextInt(itemCount)));
        }

        return randomItems;
    }

    private static void createPackages() {

         System.err.println("CREATING PACKAGES");

         List<Category> categories 
                 = JPAController.CategoryJpaController.findCategoryEntities();
         int catCount = categories.size();

         Package newPackage;

         Calendar c = Calendar.getInstance();

         for (int i = 0; i < 50; i++) {
             newPackage = new Package();
             newPackage.setName("Package " + i);
             newPackage.setCategory(categories.get(
                     randomizer.nextInt(catCount)));
             newPackage.setCount(randomizer.nextInt(4));
             newPackage.setDescription("Description package " + i);
             newPackage.setDiscount(randomizer.nextDouble() * 10);             
             newPackage.setPrice(randomizer.nextDouble() * 100);

             c.set(2009, randomizer.nextInt(10) + 1, randomizer.nextInt(27) + 1,
                     randomizer.nextInt(24), randomizer.nextInt(59), 0);

             newPackage.setValidity(c.getTime());

            try {
                JPAController.PackageJpaController.create(newPackage);
            } catch (Exception ex) {
                Logger.getLogger(DBFiller.class.getName()).log(
                        Level.SEVERE, null, ex);
            }

            List<Item> itemList = getRandomItems();
            List<ItemPackage> itemPackageList = new ArrayList<ItemPackage>();

            for (Item i1 : itemList) {
                ItemPackage ip = new ItemPackage();
                ip.setPackage1(newPackage);
                ip.setItem(i1);
                ip.setItemCount(1);
                try {
                    JPAController.ItemPackageJpaController.create(ip);
                } catch (PreexistingEntityException ex) {
                    Logger.getLogger(DBFiller.class.getName()).log(Level.SEVERE, null, ex);
                } catch (Exception ex) {
                    Logger.getLogger(DBFiller.class.getName()).log(Level.SEVERE, null, ex);
                }

                itemPackageList.add(ip);
            }

            newPackage.setItemPackageCollection(itemPackageList);
            
            try {
                JPAController.PackageJpaController.edit(newPackage);
            } catch (IllegalOrphanException ex) {
                Logger.getLogger(DBFiller.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NonexistentEntityException ex) {
                Logger.getLogger(DBFiller.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                Logger.getLogger(DBFiller.class.getName()).log(Level.SEVERE, null, ex);
            }
         }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        createUnits();
        createCategories();
        createItems();
        createPackages();

    }

}
